//
//  JSQMessage+ICT.m
//  iCareTek
//
//  Created by Magic on 15/6/18.
//  Copyright (c) 2015年 iCareTech. All rights reserved.
//

#import "JSQMessage+ICT.h"
#import "ICTMsgContentEntity.h"
#import "YWKBaseMessageEty.h"
#import <objc/runtime.h>
#import "ICTChatMessageEntity.h"
#import "DDLog.h"
#import "YWKLoginHander.h"
#import "YWKUser.h"

#ifdef DEBUG
static int ddLogLevel = LOG_LEVEL_VERBOSE;
#else
static int ddLogLevel = LOG_LEVEL_OFF;
#endif

static NSString const *JSQMessageICT = @"JSQMessageICT";

static NSString const *JSQMessageType = @"JSQMessageType";

static NSString const *JSQMessageVoiceSize = @"JSQMessageVoiceSize";

static NSString const *JSQMessageCtrlType = @"JSQMessageCtrlType";

static NSString const *JSQMessageTitle = @"JSQMessageTitle";

static NSString const *JSQMessageSourceId = @"JSQMessageSourceId";

static NSString const *JSQMessageSourceType = @"JSQMessageSourceType";

static NSString const *JSQMessageUrl = @"JSQMessageUrl";

static NSString const *JSQMessageContent = @"JSQMessageContent";


static NSString const *JSQDialogueId = @"JSQDialogueId";
static NSString const *JSQParentId   = @"JSQParentId";
static NSString const *JSQThumbUrl   = @"JSQThumbUrl";
static NSString const *JSQOriginUrl  = @"JSQOriginUrl";
static NSString const *JSQVoiceUrl   = @"JSQVoiceUrl";


@implementation JSQMessage (ICT)

- (void)setFileName:(NSString *)fileName
{
    [self willChangeValueForKey:@"fileName"];
    objc_setAssociatedObject(self, &JSQMessageICT, fileName, OBJC_ASSOCIATION_COPY);
    [self didChangeValueForKey:@"fileName"];
}

- (NSString *)fileName {
    return objc_getAssociatedObject(self, &JSQMessageICT);
}

- (void)setMessageType:(NSNumber *)messageType
{
    [self willChangeValueForKey:@"messageType"];
    objc_setAssociatedObject(self, &JSQMessageType, messageType, OBJC_ASSOCIATION_RETAIN);
    [self didChangeValueForKey:@"messageType"];
}

- (NSNumber *)messageType
{
  return objc_getAssociatedObject(self, &JSQMessageType);
}

- (NSString *)title
{
    return objc_getAssociatedObject(self, &JSQMessageTitle);
}

- (void)setTitle:(NSString *)title
{
    [self willChangeValueForKey:@"title"];
    objc_setAssociatedObject(self, &JSQMessageTitle, title, OBJC_ASSOCIATION_COPY);
    [self didChangeValueForKey:@"title"];
}

- (NSString *)content
{
    return objc_getAssociatedObject(self, &JSQMessageContent);
}

- (void)setContent:(NSString *)content
{
    [self willChangeValueForKey:@"content"];
    objc_setAssociatedObject(self, &JSQMessageContent, content, OBJC_ASSOCIATION_COPY);
    [self didChangeValueForKey:@"content"];
}

- (NSString *)sourceId
{
    return objc_getAssociatedObject(self, &JSQMessageSourceId);
}

- (void)setSourceId:(NSString *)sourceId
{
    [self willChangeValueForKey:@"sourceId"];
    objc_setAssociatedObject(self, &JSQMessageSourceId, sourceId, OBJC_ASSOCIATION_COPY);
    [self didChangeValueForKey:@"sourceId"];
}

- (NSString *)sourceType
{
    return objc_getAssociatedObject(self, &JSQMessageSourceType);
}

- (void)setSourceType:(NSString *)sourceType
{
    [self willChangeValueForKey:@"sourceType"];
    objc_setAssociatedObject(self, &JSQMessageSourceType, sourceType, OBJC_ASSOCIATION_COPY);
    [self didChangeValueForKey:@"sourceType"];
}

- (NSString *)url
{
    return objc_getAssociatedObject(self, &JSQMessageUrl);
}

- (void)setUrl:(NSString *)url
{
    [self willChangeValueForKey:@"url"];
    objc_setAssociatedObject(self, &JSQMessageUrl, url, OBJC_ASSOCIATION_COPY);
    [self didChangeValueForKey:@"url"];
}

- (void)setMessageVoiceSize:(NSNumber *)messageVoiceSize
{
    [self willChangeValueForKey:@"messageVoiceSize"];
    objc_setAssociatedObject(self, &JSQMessageVoiceSize, messageVoiceSize, OBJC_ASSOCIATION_RETAIN);
    [self didChangeValueForKey:@"messageVoiceSize"];
}

- (NSNumber *)messageVoiceSize
{
    return objc_getAssociatedObject(self, &JSQMessageVoiceSize);
}


- (void)setMessageCtrlType:(NSNumber *)messageCtrlType
{
    [self willChangeValueForKey:@"messageCtrlType"];
    objc_setAssociatedObject(self, &JSQMessageCtrlType, messageCtrlType, OBJC_ASSOCIATION_RETAIN);
    [self didChangeValueForKey:@"messageCtrlType"];
}

- (NSNumber *)messageCtrlType
{
    return objc_getAssociatedObject(self, &JSQMessageCtrlType);
}


- (void)setDialogueId:(NSNumber *)dialogueId
{
    [self willChangeValueForKey:@"dialogueId"];
    objc_setAssociatedObject(self, &JSQDialogueId, dialogueId, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self didChangeValueForKey:@"dialogueId"];
}
- (NSNumber *)dialogueId
{
    return objc_getAssociatedObject(self, &JSQDialogueId);
}

- (void)setParentId:(NSNumber *)parentId
{
    [self willChangeValueForKey:@"parentId"];
    objc_setAssociatedObject(self, &JSQParentId, parentId, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self didChangeValueForKey:@"parentId"];
}
- (NSNumber *)parentId
{
//    return 12;
    //  todo
    return objc_getAssociatedObject(self, &JSQParentId);
}

- (void)setThumbUrl:(NSString *)thumbUrl
{
    [self willChangeValueForKey:@"thumbUrl"];
    objc_setAssociatedObject(self, &JSQThumbUrl, thumbUrl, OBJC_ASSOCIATION_COPY);
    [self didChangeValueForKey:@"thumbUrl"];
}
- (NSString *)thumbUrl
{
    return objc_getAssociatedObject(self, &JSQThumbUrl);
}

- (void)setOriginUrl:(NSString *)originUrl
{
    [self willChangeValueForKey:@"originUrl"];
    objc_setAssociatedObject(self, &JSQOriginUrl, originUrl, OBJC_ASSOCIATION_COPY);
    [self didChangeValueForKey:@"originUrl"];
}
- (NSString *)originUrl
{
   return objc_getAssociatedObject(self, &JSQOriginUrl);
}

- (void)setVoiceUrl:(NSString *)voiceUrl
{
    [self willChangeValueForKey:@"voiceUrl"];
    objc_setAssociatedObject(self, &JSQVoiceUrl, voiceUrl, OBJC_ASSOCIATION_COPY);
    [self didChangeValueForKey:@"voiceUrl"];
}
- (NSString *)voiceUrl
{
   return objc_getAssociatedObject(self, &JSQVoiceUrl);
}



- (ICTChatMessageEntity *)transToICTMessageWithFriend:(XMPPJID *)jid isForConsult:(BOOL)isForConsult
{
    ICTChatMessageEntity *msg = [[ICTChatMessageEntity alloc] init];
    msg.from = [[[YWKLoginHander loginHandler].account jid] bare];
    msg.to = jid.bare;
    
    if (self.isMediaMessage) {
        if ([self.messageType integerValue] == ICTMessageTypeImage) {
            YWKBaseMessageEty *content = [[YWKBaseMessageEty alloc]initWithImagePath:self.fileName];
            content.receiver = jid.user;
            content.sender  = [[[YWKLoginHander loginHandler].account jid] bare];
            msg.messageStruct = content;
        }
        else if ([self.messageType integerValue] == ICTMessageTypeVoice)
        {
           YWKBaseMessageEty * content = [[YWKBaseMessageEty alloc]initWithVoicePath:self.fileName
                                                                                 voiceSize:[self.messageVoiceSize integerValue]];
            content.receiver = jid.user;
            content.sender  = [YWKLoginHander loginHandler].account.userName;
            msg.messageStruct = content;
        }
        else{
            DDLogInfo(@"message is not a media");
        }
        msg.type = [self.messageType integerValue];
    }
    else
    {
        if ([self.messageType integerValue] == ICTMessageTypeDictorRichMsg)
        {
            YWKBaseMessageEty *content = [[YWKBaseMessageEty alloc] initWithVisitTitle:self.title
                                                                               content:self.content
                                                                                   url:self.url
                                                                              sourceId:[self.sourceId integerValue]
                                                                            sourceType:[self.sourceType integerValue]];
            content.receiver = jid.user;
            content.sender  = [YWKLoginHander loginHandler].account.userName;
            msg.messageStruct = content;
            msg.type = ICTMessageTypeDictorRichMsg;
            
        }
        else
        {
            YWKBaseMessageEty *content = [[YWKBaseMessageEty alloc] initWithText:self.text];
            content.receiver = jid.user;
            content.sender  = [YWKLoginHander loginHandler].account.userName;
            msg.messageStruct = content;
            msg.type = ICTMessageTypePlain;
        }
    }
    
    msg.time = self.date?self.date:[NSDate date];
    msg.isRead = 1;
    return msg;
}

+ (JSQMessage *)messageWithSenderId:(NSString *)senderId
                        displayName:(NSString *)displayName
                           fileName:(NSString *)fileName
                              media:(id<JSQMessageMediaData>)media
{
    JSQMessage *msg = [JSQMessage messageWithSenderId:senderId
                                          displayName:displayName
                                                media:media];
    msg.fileName = fileName;
    return msg;
}

@end
